package com.ta.gulimall.coupon.service.impl;

import com.ta.gulimall.common.view.to.SkuFullReductionTo;
import com.ta.gulimall.common.view.to.json.MemberPrice;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ta.gulimall.coupon.dao.MemberPriceDao;
import com.ta.gulimall.coupon.entity.MemberPriceEntity;
import com.ta.gulimall.coupon.service.MemberPriceService;


@Service("memberPriceService")
public class MemberPriceServiceImpl extends ServiceImpl<MemberPriceDao, MemberPriceEntity> implements MemberPriceService {

    @Override
    public void saveMemberPrices(SkuFullReductionTo skuFullReductionTo) {
        List<MemberPriceEntity> memberPrices = skuFullReductionTo.getMemberPrice().stream()
                .filter(f -> f.getPrice().compareTo(new BigDecimal(0)) > 0)
                .map(m -> getMemberPriceEntity(m, skuFullReductionTo)).collect(Collectors.toList());

        this.saveBatch(memberPrices);
    }

    private MemberPriceEntity getMemberPriceEntity(MemberPrice m, SkuFullReductionTo skuFullReductionTo) {
        MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
        memberPriceEntity.setSkuId(skuFullReductionTo.getSkuId());
        memberPriceEntity.setMemberLevelId(m.getId());
        memberPriceEntity.setMemberLevelName(m.getName());
        memberPriceEntity.setMemberPrice(m.getPrice());
        return memberPriceEntity;
    }
}